package com.shiguo365.market.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.github.javiersantos.bottomdialogs.BottomDialog;
import com.shiguo365.market.display.activity.WebViewActivity;

/**
 * Created by caomenglong on 16/8/20.
 */
public class TongZhiReceiver extends BroadcastReceiver {
    private Context mActivity;
    private String TAG="TongZhiReceiver";

    public TongZhiReceiver(Context mActivity) {
        this.mActivity = mActivity;
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction());
        String title =intent.getStringExtra("title");
        String contentext = bundle.getString("content");
        String extras = bundle.getString("extras");
        int notificationId = bundle.getInt("notificationId");
        ShowDialog(title,contentext,extras);

    }

    public void ShowDialog(String title,String contentext,String extras){


        Log.d(TAG,extras);
        if(extras=="" || extras == null){
            extras="{}";
        }

        JSONObject myJsonObject =JSONObject.parseObject(extras);
        int mold=0;  // 0普通消息 1web消息
        String positiveText="已读";
        String url="";
        try {
            if(myJsonObject.getString("URL")!=null){
                url=myJsonObject.getString("URL");
                mold=1;
                positiveText="查看详情";
            }
        }catch (Exception e){

        }

        final int finalMold = mold;
        final String finalUrl=url;
        new BottomDialog.Builder(mActivity)
                .setTitle(title)
                //.setCancelable(false)
                .setContent(contentext)
                .setPositiveText(positiveText)
                .onPositive(new BottomDialog.ButtonCallback() {
                    @Override
                    public void onClick(BottomDialog dialog) {
                        if(finalMold ==1){
                            Intent intent =new Intent(mActivity,WebViewActivity.class);
                            intent.putExtra("url",finalUrl);
                            mActivity.startActivity(intent);
                        }
                    }
                })
                .show();
    }
}
